//
//  ZLExciseResultViewController.m
//  ZLExamination
//
//  Created by Lei_Zhong_Xun on 2017/8/23.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLExciseResultViewController.h"
#import "ZLExciseResultHeaderView.h"
#import "ZlExciseResultTableViewCell.h"
#import "ZLExciseModel.h"
#import "ZLWrongParsingViewController.h"
#import "ZLExciseBeginViewController.h"

@interface ZLExciseResultViewController ()
@property (nonatomic, strong) ZLExciseResultHeaderView *resultView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) ZLExciseModel *model;

@end

@implementation ZLExciseResultViewController


- (void)goToWrongParsingAction:(UIControl *)ctrl {

#pragma mark - 跳转错题解析
    if (_model.wrongAnswes > 0) {
        ZLWrongParsingViewController *wpVC = [[ZLWrongParsingViewController alloc] init];
        wpVC.navigationItem.title = _model.chapter_name;
        wpVC.curriculum_id = _model.curriculum_id;
        wpVC.answersDataArr = _model.wrongAnswes;
        [self.navigationController pushViewController:wpVC animated:YES];
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您全部答对,没有错题" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"练习结果";
    [self.view addSubview:self.footerView];
    self.myTableView.tableHeaderView = self.resultView;
    self.myTableView.bounces = NO;
    [self.myTableView registerClass:[ZlExciseResultTableViewCell class] forCellReuseIdentifier:ZlExciseResultTableViewCellID];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self showBackButtonWithImage:@"exam_back"];
    
    [self loadData];
}

#pragma mark - loaddata
- (void)loadData {
    NSDictionary *param = @{@"curriculum_id":_curriculum_id,
                            @"chapter_id":_chapter_id};
    
    [[YCRequestDataService sharedManager] Post:@"curriculum/exercises/exercises_score" parameters:param success:^(id response) {
        if (response[@"error_msg"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            _model = [[ZLExciseModel alloc] initWithDataDic:response[@"response_data"][@"lists"]];
            _resultView.model = _model;
            [self.myTableView reloadData];
        }
    } failure:^{
        NSLog(@"网络或服务器出错");
    }];
    
}

#pragma mark - 点击事件
- (void)footerViewClick:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"再练一次"]) {
        kCX_LOG(@"再练一次");
        //TODO:
        ZLExciseBeginViewController *exciseVC = [[ZLExciseBeginViewController alloc] init];
        exciseVC.model = _oneMoreExcisemodel;
        [self.navigationController pushViewController:exciseVC animated:YES];
    } else if ([sender.titleLabel.text isEqualToString:@"重新学习"]) {
        kCX_LOG(@"重新学习");
        //TODO:
        [self dismissViewControllerAnimated:YES completion:^{}];
    } else {
//        kCX_LOG(@"下一章");
//        //TODO:
//        [self dismissViewControllerAnimated:YES completion:^{}];
    }
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZlExciseResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ZlExciseResultTableViewCellID forIndexPath:indexPath];
    
    if (_model.answers) {
        cell.models = _model.answers[indexPath.section];
    }
    
    return cell;
}
#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_model.wrongAnswes) {
        CGFloat a = _model.answers[indexPath.section].count/5.;
        NSInteger b = _model.answers[indexPath.section].count/5;
        NSInteger rows = a>b? b+1: b;
        return rows*(kSCREEN_WIDTH/5);
    }
    
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        UIControl *headerView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 30)];
        UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, 30)];
        titleLbl.text = @"练习结果";
        titleLbl.font = kMyFont(13);
        titleLbl.textColor = [UIColor darkGrayColor];
        [headerView addSubview:titleLbl];
        
        UILabel *subTitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(kSCREEN_WIDTH-120, 0, 100, 30)];
        subTitleLbl.textAlignment = NSTextAlignmentRight;
        subTitleLbl.font = kMyFont(13);
        subTitleLbl.text = @"错题解析";
        [headerView addSubview:subTitleLbl];
        
        [headerView addTarget:self action:@selector(goToWrongParsingAction:) forControlEvents:UIControlEventTouchUpInside];
        
        return headerView;
    } else {
        return nil;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 30;
    } else {
        return .001;
    }
}



#pragma mark - 布局
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [_footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(100);
        make.left.right.bottom.equalTo(self.view);
    }];
    [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(- 100);
    }];
}

#pragma mark - lazy load
- (ZLExciseResultHeaderView *)resultView
{
    if (!_resultView) {
         _resultView = [[ZLExciseResultHeaderView alloc] initWithFrame:(CGRect){0,0,kSCREEN_WIDTH,200}];
    }
    return _resultView;
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] init];
        _footerView.backgroundColor = [UIColor whiteColor];
        
        NSInteger buttonWidth = (kSCREEN_WIDTH-19 * 2 - 48 * 2)/3;
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.backgroundColor = kMY_BLUE_COLOR;
        btn1.layer.cornerRadius = 5;
        btn1.layer.masksToBounds = YES;
        btn1.frame = (CGRect){70,0,buttonWidth,80};
        [btn1 setTitle:@"再练一次" forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(footerViewClick:) forControlEvents:UIControlEventTouchUpInside];
        btn1.titleLabel.font = kMyFont(15);
        [_footerView addSubview:btn1];
        
        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame = (CGRect){kSCREEN_WIDTH-70-buttonWidth,0,buttonWidth,80};
        btn2.backgroundColor = kMY_BLUE_COLOR;
        btn2.layer.cornerRadius = 5;
        btn2.layer.masksToBounds = YES;
        [btn2 setTitle:@"重新学习" forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(footerViewClick:) forControlEvents:UIControlEventTouchUpInside];
        btn2.titleLabel.font = kMyFont(15);
        [_footerView addSubview:btn2];
        
//        UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn3.frame = (CGRect){19 + (buttonWidth + 48) * 2,0,buttonWidth,80};
//        btn3.backgroundColor = kMY_BLUE_COLOR;
//        btn3.layer.cornerRadius = 5;
//        btn3.layer.masksToBounds = YES;
//        [btn3 setTitle:@"下一章" forState:UIControlStateNormal];
//        [btn3 addTarget:self action:@selector(footerViewClick:) forControlEvents:UIControlEventTouchUpInside];
//        btn3.titleLabel.font = kMyFont(15);
//        [_footerView addSubview:btn3];
    }
    return _footerView;
}
- (void)backBarButtonPressed:(id)sender {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kExerciseResultDismissNotification" object:nil];
    
    [self dismissViewControllerAnimated:YES completion:^{

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
